<?php

namespace App\Admin\Controllers;

use App\Repositories\UserRepository;
use App\User;

use App\UserTeam;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use PHPUnit\Util\Log\TeamCity;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户列表');
            $content->description('用户列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {

        return Admin::content(function (Content $content) use ($id) {
            $content->header('header');
            $content->description('description');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('header');
            $content->description('description');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $userTeam=UserTeam::pluck('name','level')->toArray();
        return Admin::grid(User::class, function (Grid $grid) use($userTeam) {
            $grid->model()->join('user_teams','user_teams.level','=','users.team_level_id')
                ->select('users.name','users.mobile','users.headimgurl','user_teams.name as level_name','users.created_at','users.team_leader','users.id')->orderBy('users.id','desc');
            $grid->filter(function($filter)use($userTeam){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->where(function($query){
                    $query->where('users.id','=',$this->input);
                },'用户id');
                $filter->where(function($query){
                    $query->where('users.name', 'like', "%{$this->input}%");
                },'名称');
                $filter->equal('mobile','手机号');
                $filter->where(function ($query) {
                    if($this->input==1){
                        $query->where('team_leader', '!=', null);
                    }elseif($this->input==2){
                        $query->where('team_leader','=',null);
                    }
                }, '推关/直关')->select([1=>'推关',2=>'直关']);
                $filter->equal('team_level_id','会员等级')->select($userTeam);
                $filter->between('created_at', '注册时间')->datetime();
            });

            $grid->id('ID')->sortable();
            $grid->name('用户名称');
            $grid->headimgurl('头像')->display(function($value){
                return "<img src='$value' width='100px'>";
            });

            $grid->team_leader('分享人')->display(function($value){
                $leader=User::find($value);
                if($leader){
                    return '昵称:'.$leader->name.'</br>'.'id:'.$leader->id.'</br>'.'手机号'.$leader->mobile;
                }else{
                    return '无推广人';
                }

            });
            $grid->mobile('手机号');
            $grid->level_name('会员等级');
            $grid->created_at('注册时间');

            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });

            $grid->tools(function($tools){
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });

            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        return Admin::form(User::class, function (Form $form) {
            $form->text('name','名称')->rules('required',[
                'required'=>'用户名不能为空'
            ]);
            $form->select('team_level_id','用户等级')->options(UserTeam::pluck('name','level'));
            $form->switch('is_regrade', '是否不能降级');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->saved(function (Form $form) {
                $user=new UserRepository();
                $user->team_level_notice(User::find($form->model()->id));

            });
        });
    }
}
